package edu.ntu.cloudroid.nongui.com;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import edu.ntu.cloudroid.model.UserData;
import edu.ntu.cloudroid.model.data.AbstractData;
import edu.ntu.cloudroid.model.data.DetachedData;
import edu.ntu.cloudroid.model.data.LocationData;
import edu.ntu.cloudroid.model.data.LocationInfo;
import edu.ntu.cloudroid.model.data.TraceData;
import edu.ntu.cloudroid.model.data.VitalSignData;
import edu.ntu.cloudroid.server.RequestServlet;
import edu.ntu.cloudroid.server.UploadServlet;


public class Communicator {
	private IClient mClient;
	private String mHostname;
	private int mPort;
	String url;
	boolean isURL;
	
	public Communicator(IClient client, String hostname, int port) {
		mClient = client;
		mHostname = hostname;
		mPort = port;
		this.isURL = false;
	}

	public Communicator(String url){
		this.url = url;
		this.isURL = true;
	}
	public void enablePush() {
		// TODO
	}

	public void sendData(UserData data) {		
		try {
			HttpURLConnection connection = getConnection(UploadServlet.METHOD_NAME);

			ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream());

			oos.writeObject(data);
			oos.flush();
			oos.close();
			connection.connect();
			//System.out.println("connect");
			connection.getInputStream();

			connection.disconnect();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//System.exit(1);
		}
	}

	public AbstractResponse getData(RequestNoInputs request) {
		AbstractResponse response = null;
		try {
			HttpURLConnection connection = getConnection(RequestServlet.METHOD_NAME);
			ObjectOutputStream oos = new ObjectOutputStream(
					connection.getOutputStream());

			oos.writeObject(request);
			oos.close();
			connection.connect();
			ObjectInputStream ois = new ObjectInputStream(
					connection.getInputStream());

			response = (AbstractResponse) ois.readObject();
			ois.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return response;
	}

	public UserData getMetaData(RequestNoInputs request) {
		UserData response = null;
		try {
			HttpURLConnection connection = getConnection(RequestServlet.METHOD_NAME);

			ObjectOutputStream oos = new ObjectOutputStream(
					connection.getOutputStream());

			oos.writeObject(request);
			oos.close();
			connection.connect();
			ObjectInputStream ois = new ObjectInputStream(
					connection.getInputStream());

			response = (UserData) ois.readObject();
			ois.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return response;
	}
	
	private HttpURLConnection getConnection(String servletName)
			throws MalformedURLException, IOException {
		HttpURLConnection connection;
		if (!this.isURL)
			connection = (HttpURLConnection) new URL(
				String.format("http://%s:%d/%s", mHostname, mPort, servletName))
				.openConnection();
		else{
			connection = (HttpURLConnection) new URL(this.url+"/"+servletName).openConnection();
			
		}
		connection.setRequestMethod("POST");
		connection.setDoInput(true);
		connection.setDoOutput(true);

		return connection;
	}	
}
